//
//  88_合并两个有序数组.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/3/8.
//

import UIKit

class HeBingArray: NSObject {
    
    func QA() {
        var array1 = [1, 3, 5, 0, 0, 0]
        let array2 = [2, 4, 6]
        merge(&array1, 3, array2, 3)
    }
    
    func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
        
        var index1 = m - 1
        var index2 = n - 1
        var countIndex = nums1.count - 1
        while index2 >= 0 {
            if index1 >= 0 && nums1[index1] > nums2[index2] {
                nums1[countIndex] = nums1[index1]
                index1 -= 1
            } else {
                nums1[countIndex] = nums2[index2]
                index2 -= 1
            }
            countIndex -= 1
        }
    }
}
